﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace HzyAdmin.Areas.Admin.Controllers.Base
{
    //
    using Model;
    using BLL;
    using DbFrame.Class;
    using Common;
    using System.Collections;
    using Microsoft.AspNetCore.Hosting;
    using System.IO;

    /// <summary>
    /// 会员管理
    /// </summary>
    public class MemberController : BaseController
    {
        private IHostingEnvironment _IHostingEnvironment = null;
        private string _WebRootPath = string.Empty;
        public MemberController(IHostingEnvironment IHostingEnvironment)
        {
            this._IHostingEnvironment = IHostingEnvironment;
            _WebRootPath = this._IHostingEnvironment.WebRootPath;
        }

        protected override void Init()
        {
            this.MenuID = "A-100";
            this.PrintTitle = "我是一个 打印标题！";
        }

        MemberBL _MemberBL = new MemberBL();

        #region  查询数据列表
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [NonAction]
        public override Sys_PagingEntity GetPagingEntity(Hashtable query, int page = 1, int rows = 20)
        {
            //获取列表
            return _MemberBL.GetDataSource(query, page, rows);
        }
        #endregion  查询数据列表

        #region  基本操作，增删改查
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        [AOP.AopCheckEntityFilter(new string[] { "model" })]
        [HttpPost]
        public ActionResult Save(MemberM model, List<Microsoft.AspNetCore.Http.IFormFile> Member_Photo_Files)
        {

            //判断是否有文件上传上来
            if (Member_Photo_Files.Count > 0)
            {
                foreach (var item in Member_Photo_Files)
                {
                    string ExtensionName = System.IO.Path.GetExtension(item.FileName).ToLower().Trim();//获取后缀名
                    if (!ExtensionName.Equals(".jpg") && !ExtensionName.Equals(".gif") && !ExtensionName.Equals(".png"))
                        throw new MessageBox("请上传后缀名为：.jpg、.gif、.png 格式的文件");
                    if (!System.IO.Directory.Exists(_WebRootPath + "\\Content\\UpFile\\"))
                        System.IO.Directory.CreateDirectory(_WebRootPath + "\\Content\\UpFile\\");
                    string filePath = "/Content/UpFile/" + Guid.NewGuid() + ExtensionName;
                    // 创建新文件
                    using (FileStream fs = System.IO.File.Create(_WebRootPath + filePath))
                    {
                        item.CopyToAsync(fs);
                        // 清空缓冲区数据
                        fs.Flush();
                    }
                    model.Member_Photo = filePath;
                }
            }
            
            li = _MemberBL.Save(model);
            this.KeyID = model.Member_ID.GetGuidString();
            if (!db.Commit(li))
                throw new MessageBox(db.ErrorMessge);
            return Json(new { status = 1, ID = KeyID });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string ID)
        {
            if (!db.Commit(_MemberBL.Delete(ID)))
                throw new MessageBox(db.ErrorMessge);
            return Json(new { status = 1 });
        }

        /// <summary>
        /// 查询根据ID
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Find(string ID)
        {
            return Json(_MemberBL.Find(ID.GetGuid()));
        }
        #endregion  基本操作，增删改查










    }
}